package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.umeng.analytics.MobclickAgent;

public class X3AloneSendMsgDialog extends Dialog {
    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private TextView tv_title;
    private TextView tv_content;
    private Button btn_cancel;
    private Button btn_affirm;

    private RelativeLayout rl_input;
    private EditText edt_input;
    private ImageView iv_del;
    private LinearLayout ll_error_tip;

    public long workBaseId;

    public String carNum = "";


    private boolean isCancel;


    /**
     * 没有按钮的提示dialog
     */
    public X3AloneSendMsgDialog(Activity arg) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_alone_share_msg_dialog);
        initDialogWindow();
        initView();
        setCanceledOnTouchOutside(false);
    }

    void initView() {
        btn_affirm = findViewById(R.id.btn_affirm);
        btn_cancel = findViewById(R.id.btn_cancel);
        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);

        rl_input = findViewById(R.id.rl_input);
        edt_input = findViewById(R.id.edt_input);
        iv_del = findViewById(R.id.iv_del);
        ll_error_tip = findViewById(R.id.ll_error_tip);

        String title = String.format(mRootActivity.getString(R.string.x3_alone_share_top_title),carNum);
        if (X3Constent.isChina()) {
            if (!X3StringUtils.isEmpty(carNum) && carNum.length() > 1){
                SpannableString contentSpan = new SpannableString(title);
                contentSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                contentSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#313340")), 2, carNum.length() + 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                contentSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), carNum.length() + 4, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tv_content.setText(contentSpan);
            } else {
                tv_content.setText(title);
            }
        } else {
            tv_content.setText(title);
        }

        iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_input.setText("");
                iv_del.setVisibility(View.GONE);
                ll_error_tip.setVisibility(View.INVISIBLE);
            }
        });

        edt_input.setFocusable(true);
        edt_input.setFocusableInTouchMode(true);
        edt_input.requestFocus();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager)edt_input.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);    //InputMethodManager.SHOW_FORCED
            }
        },200);

        edt_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {

                if (!X3StringUtils.isEmpty(edt_input.getText().toString().trim())) {
                    iv_del.setVisibility(View.VISIBLE);
                } else {
                    iv_del.setVisibility(View.GONE);
                }
                ll_error_tip.setVisibility(View.INVISIBLE);

            }
        });

        btn_cancel.setOnClickListener(view -> dismiss());
        btn_affirm.setOnClickListener(view -> {
            if (isCancel) {
                return;
            }
            if (checkNum()) {
                ll_error_tip.setVisibility(View.VISIBLE);
                X3ToastUtils.showMessage(mRootActivity.getString(R.string.x3_input_yes_phone));
                return;
            }
            if (iBtnClick != null) {
                iBtnClick.iBtnAffirmClick(edt_input.getText().toString().trim());
            }
        });
    }

    Handler handler = new Handler();

    private boolean checkNum() {
        boolean flag = false;
        if (X3Constent.isChina()) {
            if (edt_input.getText().toString().trim().length() != 11) {
                flag = true;
            }
        } else {
            if (edt_input.getText().toString().trim().length() <= 2) {
                flag = true;
            }
        }
        return flag;
    }

    IBtnClick iBtnClick;

    public void setiBtnClick(IBtnClick iBtnClick) {
        this.iBtnClick = iBtnClick;
    }

    public interface IBtnClick {
        void iBtnAffirmClick(String mobile);
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.765);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }


    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("是否全部通过弹窗");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        MobclickAgent.onPageEnd("是否全部通过弹窗");
    }
}

